//
//  BSKDBManager.m
//  TextNote
//
//  Created by rimi on 16/5/23.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKDBManager.h"


@interface BSKDBManager ()
@end

@implementation BSKDBManager

-(FMDatabase *)openDataBase:(NSString *)aDBName{
	_DBName = aDBName;
	NSString * dbPath = [NSString stringWithFormat:@"%@/%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],aDBName];
	FMDatabase * fmdb = [FMDatabase databaseWithPath:dbPath];
	NSLog(@"数据库路径：%@",dbPath);
	_fmDatabase = fmdb;
	return fmdb;
}


-(BOOL)deleteTable:(NSString *)tableName{
	NSString * SQL = [NSString stringWithFormat:@"drop table %@", tableName];
	
	if (_fmDatabase) {
		[_fmDatabase open];
		BOOL b = [_fmDatabase executeUpdate:SQL];
		[_fmDatabase close];
		if(b){
			NSLog(@"drop table [ %@ ] success!!",tableName);
			return YES;
		}
		else{
			return NO;
		}
	}
	return NO;
}

-(BOOL)creatTable:(NSString *)tableName Columnns:(NSArray *)columnns{

	NSMutableString * CreatTabel = [NSMutableString stringWithFormat:@"create table if not exists '%@' (",tableName];
	NSInteger count = columnns.count;
	for (NSInteger index = 0; index < count; index ++) {
		NSArray * array = columnns[index];
		if (index!=count-1) {
			[CreatTabel appendFormat:@" '%@' %@ ,",array[0],array[1]];
		}else{
			[CreatTabel appendFormat:@" '%@' %@ )",array[0],array[1]];
		}
	}
	if (_fmDatabase) {
		[_fmDatabase open];
		BOOL b = [_fmDatabase executeUpdate:CreatTabel];
		[_fmDatabase close];
		if(b){
			NSLog(@"Creat success");
			
			return YES;
		}
		else{
			return NO;
		}
	}
	return NO;
}

-(BOOL)insertDataToTable:(NSString * )tableName Columnns:(NSArray *)columnns{

	NSMutableString * SQL = [NSMutableString stringWithFormat:@"insert into '%@' (",tableName];
	NSMutableString * valuesStr = [NSMutableString stringWithString:@" values ("];
	NSMutableArray * values = [NSMutableArray array];
	NSInteger count = columnns.count;
	for (NSInteger index = 0; index < count; index ++) {
		NSArray * array = columnns[index];
		if (index!=count-1) {
			[SQL appendFormat:@" '%@' ,",array[0]];
			[valuesStr appendString:@"? ,"];
		}else{
			[SQL appendFormat:@" '%@' )",array[0]];
			[valuesStr appendString:@" ? )"];
		}
		[values addObject:array[1]];
	}
	
	[SQL appendString:[valuesStr copy]];
	
	if (_fmDatabase) {
		[_fmDatabase open];
		BOOL b = [_fmDatabase executeUpdate:SQL withArgumentsInArray:values];
		[_fmDatabase close];
		if(b){
			NSLog(@"insert success");
			
			return YES;
		}
		else{
			return NO;
		}
	}
	return NO;
}


-(BOOL)deleteDataFromTable:(NSString * )tableName condition:(NSString *)condition{
	NSMutableString * SQL = [NSMutableString stringWithFormat:@"delete from '%@' ",tableName];
	[SQL appendFormat:@" where %@ " ,condition];
	if (_fmDatabase) {
		[_fmDatabase open];
		BOOL b = [_fmDatabase executeUpdate:SQL];
		[_fmDatabase close];
		if(b){
			NSLog(@"delete success");
			
			return YES;
		}
		else{
			return NO;
		}
	}
	return NO;
}

-(FMResultSet *)queryDataFromTable:(NSString *) tableName Columnns:(NSArray *) columnns condition:(NSString * )condition{
	
	
	NSMutableString * SQL = [NSMutableString stringWithString:@"select "];
	if (columnns) {
		NSInteger count = columnns.count;
		for (NSInteger index = 0; index < count; index ++) {
			if (index!=count-1) {
				[SQL appendFormat:@" %@,",columnns[index]];
			}else{
				[SQL appendFormat:@" %@ ",columnns[index]];
			}
		}
	}else {
		[SQL appendString:@" * "];
	}
	[SQL appendFormat:@"from '%@'",tableName];
	if (condition) {
		[SQL appendFormat:@" where %@ " ,condition];
	}
	[_fmDatabase open];
	FMResultSet * result = [_fmDatabase executeQuery:SQL];
//	[_fmDatabase close];
	return result;
}


-(BOOL)updateDataToTable:(NSString * )tableName Columnns:(NSArray *)columnns condition:(NSString *)condition{
	NSMutableString * SQL = [NSMutableString stringWithFormat:@"update '%@' set ",tableName];
	NSMutableArray * values = [NSMutableArray array];
	NSInteger count = columnns.count;
	for (NSInteger index = 0; index < count; index ++) {
		NSArray * array = columnns[index];
		if (index!=count-1) {
			[SQL appendFormat:@" '%@' = ? ,",array[0]];
		}else{
			[SQL appendFormat:@" '%@' = ? ",array[0]];
		}
		[values addObject:array[1]];
	}
	if (condition) {
		[SQL appendFormat:@" where %@ " ,condition];
	}
	if (_fmDatabase) {
		[_fmDatabase open];
		NSError * erro = nil;
		BOOL b = [_fmDatabase executeUpdate:SQL values:values error:&erro];
		[_fmDatabase close];
		if(b){
			NSLog(@"update success");
			
			return YES;
		}
		else{
			if (erro) {
				NSLog(@"%@",erro);
			}
			return NO;
		}
	}
	return NO;
}

-(void)setAllowForeignKey:(BOOL)allowForeignKey{
	_allowForeignKey = allowForeignKey;
	NSString * SQL;
	if (allowForeignKey) {
		SQL = @"PRAGMA foreign_keys = ON ";
	}
	else{
		SQL = @"PRAGMA foreign_keys = OFF ";
	}
	if (_fmDatabase) {
	[_fmDatabase open];
		[_fmDatabase executeUpdate:SQL];
	[_fmDatabase close];
	}
}


@end
